#include <python/Python.h>
#include <stdio.h>

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return Py_BuildValue("i", sts);
}

static PyMethodDef SpamMethods[] = {
    // ...
    {"system",  spam_system, METH_VARARGS,
     "Execute a shell command."},
    // ...
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initspam(void)
{
    (void) Py_InitModule("spam", SpamMethods);
}


int
main(int argc, char *argv[])
{

 
  Py_Initialize();  //初始化
 
  PyObject* pModule = NULL;
  PyObject* pFunc = NULL;
  PyObject* pParam = NULL;
  PyObject* pResult = NULL;
  const char* pBuffer = NULL;
  int iBufferSize = 0;
 
 	/* Add a static module */
    initspam();	

  pModule = PyImport_ImportModule("test_x");
  if (!pModule)
  {
    printf("get module failed!\n");
    exit (0);
  }

  pFunc = PyObject_GetAttrString(pModule, "main");
  if (!pFunc)
  {
    printf("get func failed!\n");
    exit (0);
  }
  pParam = Py_BuildValue("(s)", "HEHEHE");
  pResult = PyEval_CallObject(pFunc,pParam);
  if(pResult)
  {
    if(PyArg_Parse(pResult, "(si)", &pBuffer, &iBufferSize))
    {
      printf("%s\n", pBuffer);
      printf("%d\n", iBufferSize);
    }
  }
  Py_DECREF(pParam);
  Py_DECREF(pFunc);
  Py_Finalize();
  printf("======\n");
  //cout << "hello" << endl;
  return 0;
}